<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="公司名" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入公司名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择审核状态"
          clearable
        >
          <el-option :key="-2" label="已驳回" :value="-1"></el-option>
          <el-option :key="-1" label="待审核" :value="0"></el-option>
          <el-option :key="1" label="已通过" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="公司类型" prop="companyType">
        <el-select
          v-model="queryParams.companyType"
          placeholder="请选择公司类型"
          clearable
        >
          <el-option :key="1" label="供应商公司" :value="1"></el-option>
          <el-option :key="2" label="需求企业" :value="2"></el-option>
          <el-option :key="3" label="开发者" :value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--<el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="companyList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->

      <el-table-column label="公司类型" align="center" prop="companyType">
        <template #default="{ row }">{{
          row.companyType === 1
            ? "供应商公司"
            : row.companyType === 2
            ? "需求企业"
            : "开发者"
        }}</template>
      </el-table-column>
      <el-table-column
        label="供应商公司名"
        align="center"
        prop="companyName"
        width="180"
        show-overflow-tooltip
      />

      <el-table-column
        label="公司地址"
        align="center"
        prop="companyAddress"
        width="180"
        show-overflow-tooltip
      />
      <el-table-column
        label="联系人"
        align="center"
        prop="companyContact"
        width="80"
      />
      <el-table-column
        label="联系电话"
        align="center"
        prop="companyContactMobile"
        width="120"
        show-overflow-tooltip
      />

      <el-table-column label="状态" align="center" width="80" prop="status">
        <template #default="{ row }">{{
          row.status === -2
            ? "驳回"
            : row.status === -1
            ? "待审核"
            : row.status === 1
            ? "通过"
            : "未知"
        }}</template>
      </el-table-column>
      <el-table-column
        label="创建人"
        align="center"
        width="80"
        prop="createBy"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        align="center"
        prop="remark"
        width="120"
        show-overflow-tooltip
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="150"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status === -1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="doReview(scope.row.sysAudit.id)"
            >审核</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改供应商公司信息对话框 -->
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      :visible.sync="open"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="供应商公司名" prop="companyName">
          <el-input
            v-model="form.companyName"
            placeholder="请输入供应商公司名"
          />
        </el-form-item>
        <el-form-item label="供应商公司地址" prop="companyAddress">
          <el-input
            v-model="form.companyAddress"
            placeholder="请输入供应商公司地址"
          />
        </el-form-item>
        <el-form-item label="供应商联系人" prop="companyContact">
          <el-input
            v-model="form.companyContact"
            placeholder="请输入供应商联系人"
          />
        </el-form-item>
        <el-form-item label="供应商联系电话" prop="companyContactMobile">
          <el-input
            v-model="form.companyContactMobile"
            placeholder="请输入供应商联系电话"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="审核"
      :visible.sync="visible"
      width="500px"
      append-to-body
      @close="reviewClose"
    >
      <el-form
        ref="reviewForm"
        :model="reviewData"
        :rules="reviewRules"
        label-width="120px"
      >
        <el-form-item label="审核结果" prop="status">
          <el-radio-group v-model="reviewData.status">
            <el-radio :label="2">通过</el-radio>
            <el-radio :label="3">驳回</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核说明" prop="auditResult">
          <el-input
            type="textarea"
            v-model="reviewData.auditResult"
            placeholder="请输入审核说明"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="confirm" :loading="confirmLoading"
          >确定</el-button
        >
        <el-button @click="visible = false">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCompany,
  getCompany,
  delCompany,
  addCompany,
  updateCompany,
} from "@/api/system/company";

import { doReview } from "@/api/system/review";

export default {
  name: "Company",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商公司信息表格数据
      companyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: null,
        status: null,
        companyType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        companyName: [{ required: true, message: "供应商公司名不能为空" }],
        companyAddress: [{ required: true, message: "供应商公司地址不能为空" }],
        companyContact: [{ required: true, message: "供应商联系人不能为空" }],
        companyContactMobile: [
          { required: true, message: "供应商联系电话不能为空" },
        ],
      },
      reviewData: {
        status: null,
        auditResult: "",
      },
      reviewRules: {
        status: { required: true, message: "请选择审核结果" },
        auditResult: { required: true, message: "请输入审核说明" },
      },
      currId: "",
      visible: false,
      confirmLoading: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    confirm() {
      this.$refs.reviewForm.validate(async (valid) => {
        if (valid) {
          this.confirmLoading = true;

          try {
            await doReview({
              id: this.currId,
              ...this.reviewData,
            });
            this.$message.success("审核成功");
            this.visible = false;
            this.getList();
          } finally {
            this.confirmLoading = false;
          }
        }
      });
    },
    reviewClose() {
      this.$refs.reviewForm.resetFields();
    },
    doReview(id) {
      this.currId = id;
      this.visible = true;
    },
    /** 查询供应商公司信息列表 */
    getList() {
      this.loading = true;
      listCompany(this.queryParams).then((response) => {
        this.companyList = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        companyId: null,
        createBy: null,
        createTime: null,
        createUserId: null,
        isDelete: null,
        remark: null,
        updateBy: null,
        updateTime: null,
        companyAddress: null,
        companyContact: null,
        companyContactMobile: null,
        companyName: null,
        status: null,
        updateUserId: null,
        companyType: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加供应商公司信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getCompany(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改供应商公司信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateCompany(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCompany(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除供应商公司信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delCompany(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/company/export",
        {
          ...this.queryParams,
        },
        `company_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
